package com.kgc.itrip.controller;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.DtoUtil;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.dto.Dto;
import com.kgc.itrip.pojo.ItripAreaDic;
import com.kgc.itrip.pojo.ItripImage;
import com.kgc.itrip.pojo.ItripLabelDic;
import com.kgc.itrip.response.ResultCode;
import com.kgc.itrip.service.ItripAreaDicService;
import com.kgc.itrip.service.ItripHotelService;
import com.kgc.itrip.service.ItripImageService;
import com.kgc.itrip.service.ItripLabelDicService;
import com.kgc.itrip.vo.ItripAreaDicVO;
import com.kgc.itrip.vo.ItripImageVO;
import com.kgc.itrip.vo.ItripLabelDicVO;
import com.kgc.itrip.vo.hotel.HotelVideoDescVO;
import com.kgc.itrip.vo.hotel.ItripSearchDetailsHotelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*酒店相关业务的控制器*/
@Api(value = "酒店业务相关的控制器")
@Slf4j
@RestController
@RequestMapping("/api/hotel")
public class HotelController {
    @Resource
    private ItripHotelService itripHotelService;
    @Resource
    private ItripAreaDicService itripAreaDicService;
    @Resource
    private ItripLabelDicService itripLabelDicService;
    @Resource
    private ItripImageService itripImageService;


    @ApiOperation(value = "根据targetId查询酒店图片(type=0)",
            httpMethod = "GET",notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
            "如下：100212 : 获取酒店图片失败 :100213 : 酒店id不能为空",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value= "/getimg/{targetId}")
        public Dto<List<ItripImageVO>> getImg(@PathVariable String targetId){
        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_TWO.getMsg(),ResultCode.HOTEL_IS_NULL_TWO.getCode());
        }
        Map<String,Object> params = new HashMap<>();
        params.put("type",Constants.IMAGE_TYPE.HOTEL_IMAGE);
        params.put("targetId",targetId);

        try{
            //调用业务逻辑层
            List<ItripImage> itripImageList = itripImageService.getItripImageListByMap(params);
            //创建放回的图片集合
            List<ItripImageVO> imageVOList = new ArrayList<>();
            for (ItripImage itripImage : itripImageList) {
                ItripImageVO imageVO = new ItripImageVO();
                BeanUtils.copyProperties(itripImage,imageVO);
                imageVOList.add(imageVO);
            }
            return DtoUtil.returnDataSuccess(imageVOList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询酒店图片失败");
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_IMAGE.getMsg(),ResultCode.HOTEL_IS_IMAGE.getCode());
        }

    }

    @ApiOperation(value = "根据酒店id查询酒店特色和介绍",
            httpMethod = "GET",notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
            "如下：10210: 酒店id不能为空 :10211: 系统异常,获取失败",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value= "/queryhoteldetails/{id}")
    public Dto<List<ItripSearchDetailsHotelVO>> queryHotelDetails(@PathVariable Integer id){
        if(id==null || id==0){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_ONE.getMsg(),ResultCode.HOTEL_IS_NULL_ONE.getCode());
        }
        try{
            //调用业务逻辑层
            List<ItripSearchDetailsHotelVO> hotelVO= itripHotelService.queryHotelDetails(Long.parseLong(String.valueOf(id)));
            return DtoUtil.returnDataSuccess(hotelVO);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询酒店特色和介绍失败");
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_ERROR_ONE.getMsg(),ResultCode.HOTEL_IS_ERROR_ONE.getCode());
        }

    }


    @ApiOperation(value = "根据酒店id查询酒店特色商圈和名称",
            httpMethod = "GET",notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
            "如下：10215: 酒店id不能为空 :10214: 系统异常,获取失败",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value= "/getvideodesc/{hotelId}")
    public Dto<HotelVideoDescVO> getVideoDesc(@PathVariable String hotelId){
        if(EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL.getMsg(),ResultCode.HOTEL_IS_NULL.getCode());
        }
        try{
            HotelVideoDescVO hotelList = itripHotelService.getVideoDesc(Long.parseLong((hotelId)));
            return DtoUtil.returnDataSuccess(hotelList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询酒店视频描述异常");
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_ERROR.getMsg(),ResultCode.HOTEL_IS_ERROR.getCode());
        }

    }



    @ApiOperation(value = "查询热门城市",
            httpMethod = "GET",notes = "查询热门城市",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value="/queryhotcity/{type}")
    public Dto<List<ItripAreaDicVO>> queryHotCity(@PathVariable Integer type){

        if(type==null || type==0){
            return DtoUtil.returnFail(ResultCode.TYPE_IS_NULL.getMsg(),ResultCode.TYPE_IS_NULL.getCode());
        }
        try{
            List<ItripAreaDicVO> hotelList = itripAreaDicService.getHotCityByCountry(type);
            return DtoUtil.returnDataSuccess(hotelList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询热门城市出错");
            return DtoUtil.returnFail(ResultCode.TYPE_IS_EXISTS.getMsg(),ResultCode.TYPE_IS_EXISTS.getCode());
        }
    }

    @ApiOperation(value = "根据城市查询商圈",
            httpMethod = "GET",
            notes = "查询国内、国外的热门城市(1:国内 2:国外)"+
                    "成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：" +
                    "错误码:"+ "10203:cityId不能为空" + "10204:系统异常,获取失败",
            protocols = "http",
            consumes = "application/json")
    @GetMapping(value = "querytradearea/{cityId}")
    public Dto<List<ItripAreaDicVO>> queryTradeArea(@PathVariable Integer cityId){
        if(cityId==null || cityId==0){
            return DtoUtil.returnFail(ResultCode.TRADEAREA_CITYID_IS_NULL.getMsg(),
                    ResultCode.TRADEAREA_CITYID_IS_NULL.getCode());
        }
        //准备需要的查询条件：
        Map<String,Object> params = new HashMap<>();
        params.put("parent",cityId);//设置城市的id
        params.put("isTradingArea", Constants.ISTRADEINGAREA.IS_TRADEAREA);//设置是否为商圈
        params.put("isActivated",Constants.ACTIVATED.IS_ACTIVATED);//设置是否激活
        try {
            List<ItripAreaDic> areaDicList = itripAreaDicService.getItripAreaDicListByMap(params);
            List<ItripAreaDicVO> voList = new ArrayList<>();
            for (ItripAreaDic itripAreaDic : areaDicList) {
                ItripAreaDicVO vo = new ItripAreaDicVO();
                BeanUtils.copyProperties(itripAreaDic,vo);
                voList.add(vo);
            }
            return DtoUtil.returnDataSuccess(voList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("城市查询商圈失败");
            return DtoUtil.returnFail(ResultCode.TRADEAREA_QUERY_IS_ERRO.getMsg(),
                    ResultCode.TRADEAREA_QUERY_IS_ERRO.getCode());
        }
    }
    @ApiOperation(value = "查询酒店特色列表",
            httpMethod = "GET",
            notes = "获取酒店特色(用于查询页列表)"+
                    "成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：" +
                    "错误码:"+ "10205: 系统异常,获取失败",
            protocols = "http",
            consumes = "application/json")
    @GetMapping(value = "/queryhotelfeature")
    public Dto<List<ItripLabelDicVO>> queryHotelFeature(){
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",16);
        try {
            List<ItripLabelDic> labelDicList = itripLabelDicService.getItripLabelDicListByMap(params);
            List<ItripLabelDicVO> voList = new ArrayList<>();
            for (ItripLabelDic itripLabelDic : labelDicList) {
                ItripLabelDicVO vo = new ItripLabelDicVO();
                //属性的对拷
                BeanUtils.copyProperties(itripLabelDic,vo);
                voList.add(vo);
            }
            return DtoUtil.returnDataSuccess(voList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("酒店特色查询失败,服务器异常");
            return DtoUtil.returnFail(ResultCode.HOTELLABEL_QUERY_IS_ERRO.getMsg(),
                    ResultCode.HOTELLABEL_QUERY_IS_ERRO.getCode());
        }
    }
}
